package com.test.wog.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.test.wog.bean.Node;
import com.test.wog.bean.RegularBus;
import com.test.wog.dao.NodeDAO;
import com.test.wog.dao.RegularBusDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 班车服务层
 *
 * @author linxun @buaa.edu.cn
 * @since 2020-01-27
 * @see RegularBus
 * @see RegularBusDAO
 */
@Service
public class RegularBusService {

    @Autowired
    RegularBusDAO regularBusDAO;
    @Autowired
    NodeDAO nodeDAO;

    /**
     * Get regular bus by type string.
     *
     * @param type the type
     * @return the string
     */
    public String getRegularBusByType(String type){//根据人员类型获取相应所有班车时刻表
        List<RegularBus> regularBusList = regularBusDAO.getRegularBusByType(type);
        List<RegularBus> regularBusListWithNodeName = new ArrayList<>();//带节点名称的班车列表
        for (RegularBus regularBus : regularBusList) {
            Node snode = nodeDAO.getNodeByID(regularBus.getOrigin());
            Node enode = nodeDAO.getNodeByID(regularBus.getDestination());
            regularBus = regularBus.toBuilder()
                    .originNode(snode)
                    .destinationNode(enode)
                    .build();
            regularBusListWithNodeName.add(regularBus);
        }
        return JSON.toJSONString(regularBusListWithNodeName, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteDateUseDateFormat);
    }

    /**
     * Insert regular bus int.
     *
     * @param regularBus the regular bus
     * @return the int
     */
    public int insertRegularBus(RegularBus regularBus) {
        return regularBusDAO.insertRegularBus(new RegularBus());
    }
}
